package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.config.R;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import com.itheima.ydd.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class YddUserInfoController {
    @Autowired
    private IYddUserService yddUserService;

    @Autowired
    private IYddArticleService yddArticleService;
    @Autowired
    private IYddUserDataService yddUserDataService;

    @GetMapping("/getUserInfo")
    public R<HashMap>getUserInfo(HttpServletRequest request){

        //获取用户登录的token
        String token = request.getHeader("token");
        boolean flag = JwtUtils.verifyToken(token);
        //判断token是否存在
        if (!flag){
            throw new BusinessException("token已失效!");
        }
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //根据用户id查询用户信息
        YddUser user = yddUserService.getById(id);
        //封装信息返回
        HashMap<String, Object> ret = new HashMap<>();
        ret.put("userInfo",user);
        return R.success(ret);
    }


    @GetMapping("/userDataHandle")
    public R userDataHandle(YddUserData yddUserData, String action,HttpServletRequest request) {
        //获取用户登录的token
        String token = request.getHeader("token");
        boolean flag = JwtUtils.verifyToken(token);
        //判断token是否存在
        if (!flag){
            throw new BusinessException("token已失效!");
        }
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        yddUserData.setUserId(id);
        LambdaQueryWrapper<YddUserData> userDataLqw = new LambdaQueryWrapper<>();
        userDataLqw.eq(YddUserData::getUserId, yddUserData.getUserId())
                .eq(YddUserData::getArticleId, yddUserData.getArticleId())
                .eq(YddUserData::getType, yddUserData.getType());
        if ("add".equals(action)) {
            yddUserDataService.save(yddUserData);
        } else {
            yddUserDataService.remove(userDataLqw);
        }
        //判断是否为点赞操作
        if ("2".equals(yddUserData.getType())){
            YddArticle yddArticle = yddArticleService.getById(yddUserData.getArticleId());
            Integer lovenum = Integer.parseInt(yddArticle.getLovenum());
            if ("add".equals(action)) {

                yddArticle.setLovenum(String.valueOf(lovenum+1));
            } else {
                yddArticle.setLovenum(String.valueOf(lovenum-1));
            }
            yddArticleService.updateById(yddArticle);
        }
        return R.success("点赞成功");
    }

    @PostMapping("/addArticle")
    public R addArticle(@RequestBody YddArticle yddArticle,HttpServletRequest request) {
        //获取用户登录的token
        String token = request.getHeader("token");
        boolean flag = JwtUtils.verifyToken(token);
        //判断token是否存在
        if (!flag){
            throw new BusinessException("token已失效!");
        }
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        yddArticle.setUserId(id);
        boolean result = yddArticleService.save(yddArticle);
        if (result) {
            return R.success("发布成功");
        }
        return R.error("发布文章失败");
    }

    @GetMapping("/delArticle")
    public R delArticle(String id) {
        boolean result = yddArticleService.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
}
